import {
  request,
  WXshowToast,
  WXRedirectTo,
  WXNavigateTo
} from '../../utils/index.js';
import {
  Api,
  getWXPayOptions,
  requestPayment
} from '../../config/api.js';
const appInst = getApp();
const {
  imgBaseUrl,
  host,
  color
} = appInst.globalData;

Page({
  data: {
    imgBaseUrl,
    host,
    color,
    addressList: [],
    // 默认地址
    addr: {},
    // [羊币，微信]
    payway: [true, false],
    goodlist: null,
    goodNum: null,
    totalMoney: 0,
  },
  onLoad: function (options) {
    console.log(options)
    //  {goodid: "14", num: "1", gift: "礼包1:香怡肉块+香郁脊骨+养生汤骨", price: "1980", title: "尊享生活"}
    this.setData({
      options
    })
    console.log(this.data.options)
  },


  onShow: function () {

    if (wx.getStorageSync('dzactive')) { //获取选中的地址 如没选中则调用默认收货地址
      let dzactive = wx.getStorageSync('dzactive')
      console.log('获取选中的地址 如没选中则调用默认收货地址')

      this.setData({
        addr: dzactive,
        addressList:dzactive
      })
      console.log(this.data.addr, '默认收货地址')
      console.log(this.data.addressList, '默认收货地址')
      // console.log(this.data.dzactive, '默认收货地址')
      // wx.removeStorageSync('dzactive')
    } else { //否则调用默认收货地址
      this._getAddressList()
    }



    if (wx.getStorageSync('gmtype')) { //如果有商品类型 则写入页面
      this.setData({
        gmtype: wx.getStorageSync('gmtype')
      })
      wx.removeStorageSync('gmtype')
    }
     this.sheep_coin()
    // this.getGood()
  },
  //   获取羊贝
  async sheep_coin() {
    const {
      data
    } = await request({
      url: Api.userAssets,
      method: 'POST'
    })

    this.setData({
      self_price:data.data.self_price
    })
    console.log(this.data.self_price)
  },
  // 获取商品想详情
  getGood() {
    let goodlist = wx.getStorageSync("goodlist");
    let totalMoney = 0
    let goodNum = 0
    if (goodlist.length > 1) {
      goodlist.forEach((item) => {

        totalMoney += item.num * Number(item.price)
        goodNum += item.num
      })
    } else {
      totalMoney = goodlist[0].price
      goodNum = 1
    }

    this.setData({
      goodlist,
      totalMoney,
      goodNum
    })
  },







  //   获取收货地址
  async _getAddressList() {
    const {
      data
    } = await request({
      url: Api.getAddressList,
      method: 'POST'
    })

    data.data.map((item, index) => {
      if (item.state === 1) {
        this.setData({
          addr: item
        })
      }
    })
    this.setData({
      addressList: data.data,
    })
  },



  async js() { //去结算
    let that = this
    const {
      totalMoney,
      deduction,
      addr
    } = this.data
    if (!Object.keys(addr).length) {
      WXshowToast("请选择收货地址")
      setTimeout(() => {
        WXNavigateTo("/pages/select_addr/select_addr")
      }, 500)
      return
    }
    let  self_price =  this.data.self_price
    console.log(+self_price)
    console.log(+this.options.price)
    // let  self_price =  0.00
    if(+self_price<+this.options.price){
      wx.showToast({
        title: '您的账户余额不足',
        icon:'none'
      })
      return
    }
    console.log(this.options)
    // let  address_id = (()=>{
    //   //  console.log(this.data.addr.id)
    //     // if(that.data.addr.id){
    //     //   return  that.data.addr.id
    //     // }else{
    //     //   WXshowToast('未填写收获地址')
    //     // }
    //  })();
    const openid = wx.getStorageSync("openid")
    const {
      options
    } = that.data
    const {
      price
    } = options //价格
    let goods_id = options.goodid //商品ID
    let pay_num = options.num //购买数量
    let gift_bag = options.gift //礼品id
    let address_id = addr.id //收货地址
    let money, order_no, pay

    const {
      data
    } = await request({
      url: Api.sheepOrder,
      // url: Api.sheepB,
      data: {
        // user_id:wx.getStorageSync('userInfo').id,
        // sheep_b:this.options.price
        goods_id, //商品Id
        price, //商品价格
        pay_num, //购买数量
        gift_bag,
        // address_id //收货地址ID
      },
      method: 'POST'
    })
    if (data) {
      if (data.code == 2001) {
        // WXRedirectTo(`/pages/choujiang/choujiang?address_id=${addr.id}`)
        this.setData({
          order_sn: data.data.order_sn
        })
      }
      if (data.code != 2001) {
        WXshowToast(data.msg)
      }
    }
    const sheepBdata = await request({
      // url: Api.sheepOrder,
      url: Api.sheepB,
      data: {
        user_id:wx.getStorageSync('userInfo').id,
        sheep_b:this.options.price,
         goods_id, //商品Id
         order_sn:data.data.order_sn,//订单Id
        // price, //商品价格
        // pay_num, //购买数量
        // gift_bag,
         address_id //收货地址ID
      },
      method: 'POST'
    })
    if (sheepBdata) {
      if (sheepBdata.data.code == 2001) {
        WXRedirectTo(`/pages/index/index`)
        // WXRedirectTo(`/pages/choujiang/choujiang?address_id=${addr.id}`)
        // this.setData({
        //   order_sn: data.data.order_sn
        // })
      }
      if (sheepBdata.data.code != 2001) {
        WXshowToast(sheepBdata.data.msg)
      }
    }

    money = price //总价格
    // order_no = this.data.order_sn //订单编号
    console.log(order_no)
    // pay = await getWXPayOptions({
    //   money,
    //   title: "肉串礼盒",
    //   order_no,
    //   openid
    // })
    // 发起微信支付
    // try {
    //   const src = await requestPayment(pay.data.data.wx_pub);
    //   WXRedirectTo(`/pages/choujiang/choujiang?address_id=${addr.id}`)
    // } catch (err) {
    //   wx.showToast({
    //     title: '支付取消',
    //     icon: 'none',
    //   });
    // }

  },
  handleChangeCount(e) {
    let num = parseInt(this.options.num)
    let num2 = num + 1
    let num1 = 'options.num'
  }
})